﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelToOracle
{
    public partial class MainForm : Form
    {
        private ImportTemplate _template;
        private ConstantValues _constantValues;

        public MainForm()
        {
            InitializeComponent();

        }

        void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem item = e.ClickedItem;
            MessageBox.Show(item.Name);
            if (item.Tag != null)
            {
                
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            // 打开模版选择窗口
            SelectTemplateDialog dlg = new SelectTemplateDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _template = dlg.SelectedTemplate;
                this.Text = "导入到" + _template.DbTable;
                ConstantFieldsSettingDialog constDlg = new ConstantFieldsSettingDialog(_template);
                if (constDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    _constantValues = constDlg.Values;
                    return;
                }
            }

            Close();
        }

        private void _ctlMenuItemImport_Click(object sender, EventArgs e)
        {
            ImportSettingDialog dlg = new ImportSettingDialog(dataGridView1);
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    dataGridView2.Rows[i].Cells[0].Value = dataGridView1.Columns[i].Visible;
                }
            }
        }

        // 打开EXCEL
        private void _ctlMenuItemOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ExcelUtil util = new ExcelUtil(dlg.FileName);
                try
                {
                    SelectSheetDialog sheetDlg = new SelectSheetDialog(util.GetSheets());
                    if (sheetDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        dataGridView1.DataSource = util.GetData(sheetDlg.SelectedSheet, _template, _constantValues);
                        foreach (DataGridViewColumn column in dataGridView1.Columns)
                        {
                            column.SortMode = DataGridViewColumnSortMode.NotSortable;
                        }
                        InitiateSettingView();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    util.Close();
                }
            }
        }

        private void InitiateSettingView()
        {
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                //ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
                //contextMenuStrip.Items.Add("显示");
                //column.ContextMenuStrip = contextMenuStrip;

                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dataGridView2);
                row.Cells[0].Value = column.Visible;
                row.Cells[1].Value = column.HeaderText;
                row.Tag = column;
                dataGridView2.Rows.Add(row);
            }
        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DataGridViewRow row = dataGridView2.Rows[e.RowIndex];
                DataGridViewColumn column = row.Tag as DataGridViewColumn;
                if ((bool)row.Cells[0].Value == true)
                {
                    column.Visible = true;
                }
                else
                {
                    column.Visible = false;
                }
            }
        }

        private void _ctlMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                row.DefaultCellStyle.BackColor = Color.Yellow;
            }
        }

        private void _ctlMenuItemClear_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                row.DefaultCellStyle.BackColor = Color.White;
            }
        }

        private void _btnMenuItemImport_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource == null)
            {
                return;
            }
            DataTable table = (dataGridView1.DataSource as DataTable).Clone();
            
            //for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
            //{
            //    if(!dataGridView1.Columns[columnIndex].Visible)
            //    {
            //        table.Columns.Remove(dataGridView1.Columns[columnIndex].HeaderText);
            //    }
            //}

            for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
            {
                if (dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor == Color.Yellow)
                {
                    DataRow row = table.NewRow();
                    for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
                    {
                        row[columnIndex] = dataGridView1.Rows[rowIndex].Cells[columnIndex].Value;
                    }
                    table.Rows.Add(row);
                }
            }

            ExportDialog dlg = new ExportDialog(_template, table);
            dlg.ShowDialog();
        }

        private void _ctlMenuItemMarkRight_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.SelectedRows)
            {
                row.Cells[0].Value = true;
                DataGridViewColumn column = row.Tag as DataGridViewColumn;
                if ((bool)row.Cells[0].Value == true)
                {
                    column.Visible = true;
                }
                else
                {
                    column.Visible = false;
                }
            }
        }

        private void _ctlMenuItemClearRight_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.SelectedRows)
            {
                row.Cells[0].Value = false;
                DataGridViewColumn column = row.Tag as DataGridViewColumn;
                if ((bool)row.Cells[0].Value == true)
                {
                    column.Visible = true;
                }
                else
                {
                    column.Visible = false;
                }
            }
        }

        private void _ctlMenuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void _ctlMenuItemExport2Excel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource == null)
            {
                return;
            }

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "excel文件(*.xlsx)|*.xlsx";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataTable table = (dataGridView1.DataSource as DataTable).Clone();

                //for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
                //{
                //    if(!dataGridView1.Columns[columnIndex].Visible)
                //    {
                //        table.Columns.Remove(dataGridView1.Columns[columnIndex].HeaderText);
                //    }
                //}

                for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
                {
                    if (dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor == Color.Yellow)
                    {
                        DataRow row = table.NewRow();
                        for (int columnIndex = 0; columnIndex < dataGridView1.Columns.Count; columnIndex++)
                        {
                            row[columnIndex] = dataGridView1.Rows[rowIndex].Cells[columnIndex].Value;
                        }
                        table.Rows.Add(row);
                    }
                }

                if (ExcelUtil.ExportExcel(table, dlg.FileName))
                {
                    MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("导出失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
